#include "Utilities.hpp"
#include <math.h>

#define PI 3.14


MVector2D mapToUnitDisk(const MVector2D& onSquare)
{
	MVector2D resVec;

	float phi, r, u, v, a, b;

	a = 2 * onSquare.x - 1;
	b = 2 * onSquare.y - 1;

	if(a > -b)
	{
		if(a > b)
		{
			r = a;
			phi = (PI/4) * (b/a);
		}
		else
		{
			r = b;
			phi = (PI/4) * (2 - (a/b));
		}
	}
	else
	{
		if(a < b)
		{
			r = -a;
			phi = (PI/4) * (4 + (b/a));
		}
		else
		{
			r = -b;
			
			if(b != 0)
				phi = (PI/4) * (6 - (a/b));
			else
				phi = 0;
		}
	}

	u = r * cos( phi );
	v = r * sin( phi );

	resVec.x = u;
	resVec.y = v;

	return resVec;
}

MVector3D mapToUnitHemisphere(const MVector2D& onCircle)
{
	MVector3D result;

	float x, y, z, r;

	x = y = z = r = 0.0f;

	r = sqrt(onCircle.x*onCircle.x + onCircle.y*onCircle.y); //r from (u,v) to center of circle
	z = 1 - r*r;
	x = onCircle.x * (sqrt(1 - z*z) / r);
	y = onCircle.y * (sqrt(1 - z*z) / r);

	result.x = x;
	result.y = y;
	result.z = z;

	return result;
}

//EOF
